import pgzrun
import time
WIDTH=1000
HEIGHT=750
snake=Actor("snake",(WIDTH//2,HEIGHT//2))
speed=7.5
pgx=speed
pgy=0

BACK_COLOR=(30,30,30)
def go():
    animate(snake,pos=(int(snake.pos[0]+pgx),int(snake.pos[1]+pgy)),duration=0.005)

def on_key_down(key):
    global pgx,pgy
    if key==keys.UP:
        pgx=0
        pgy=0-speed
    if key==keys.DOWN:
        pgx=0
        pgy=speed
    if key==keys.LEFT:
        pgx=0-speed
        pgy=0
    if key==keys.RIGHT:
        pgx=speed
        pgy=0

def update():
    go()
    if(snake.pos[0]>WIDTH-40 or snake.pos[0]<40 or snake.pos[1]>HEIGHT-40 or snake.pos[1]<40):
        time.sleep(0.5)
        animate(snake,pos=(WIDTH//2,HEIGHT//2),duration=0.005)
        pgx=3
        pgy=0

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    snake.draw()

pgzrun.go()